﻿
var uploadButtonImg = root_path +'theme/default/images/upload_'+ lang +'.png';

$(function(){
	tagSelect('.tagMenu a');
	mediaFile();
	mediaThumb();
	mediaFileSource();
	$('#uploadForm').submit(function(){ uploadSubmit(this);return false});
	addTag('#customizeTag','#addTag','#tagList');
	
	strLimit('#fileName','#fileNameNum',40,2);
	strLimit('#description','#descriptionNum',(new Date()).getFullYear(),1);
	
	if( uploadType == 'upload')
		$('#otherOption').slideDown('normal');
	else{
		$('#otherOption').slideDown('normal');
		$('#otherOptionKey').removeClass("foldedTitle").find('b').text('-');
	}
	$('#otherOptionKey').click(function(){
		$('#otherOption').slideToggle('slow')
		if($(this).hasClass("foldedTitle")){
			$(this).removeClass("foldedTitle").find('b').text('-');
		}else{
			$(this).addClass("foldedTitle").find('b').text('+');
		}
	});
	
	creatOrderNum('#fileQueue .uploadifyQueueItem');
	
	$("#fileQueue").sortable({
		stop:function(e,ui){
			creatOrderNum('#fileQueue .uploadifyQueueItem');
		}
	});
		
	$("#fileQueue .uploadifyQueueItem").live('mouseover',function(){
		$(this).addClass('hover');
	}).live('mouseout',function(){
		$(this).removeClass('hover');
	});
		
	
	cancleFile('div.uploadifyQueueItem div.cancel a','#fileQueue .uploadifyQueueItem');
	//setInterval(function(){  $('body').append('<p>1</p>');},1000);
	
	
});




function mediaFile(){
	$("#mediaFile").uploadify({
		'uploader'       : root_path +'upload/scripts/uploadify.swf',
		//'script'         : 'upload/scripts/uploadify.php',
		'script'         : root_path +'?option=uploadFileAction%26type=action%26do=uploadFile%26time='+ time(),
		'cancelImg'      : root_path+'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true ,
		'queueSizeLimit' : 50,
		'fileDesc' 		 : '*.jpg;*.jpeg;*.gif;*.png;*.flv;*.mp4;*.3gp;*.swf' , 
		'fileExt'		 : '*.jpg;*.jpeg;*.gif;*.png;*.flv;*.mp4;*.3gp;*.swf' , 
		'sizeLimit'		 : 300 * 1000 * 1024  , //byte
		'width'			 : 120,
		'height'		 : 30,	
		'buttonImg': uploadButtonImg,
		onCancel: function (a, b, c, d) {},
		onComplete: function (evt, queueID, fileObj, response, data) {
			response = replaceBom(response);
			$('#mediaFile'+queueID).attr('file',response)
				.find('span.fileName').after($('#mediaTitleHtml').html())
					.parent().append($('#mediaTitleTextarea').html());
			creatOrderNum('#fileQueue .uploadifyQueueItem');
			fileUploadComplete('#mediaFile'+queueID);
			return false;
		},
		onSelect : function(){
			if( $('#fileQueue .uploadifyQueueItem').length >= 50 ){
				return false;
			}
		}
	});
};



function mediaThumb(){
	$("#mediaThumb").uploadify({
		'uploader'       : root_path+'upload/scripts/uploadify.swf',
		//'script'         : 'jCrop/?option=cropDo%26type=uploadPhoto%26time='+ time(),
		'script'         : root_path +'?option=uploadFileAction%26type=action%26do=uploadMediaThumb%26time='+ time(),
		'cancelImg'      : root_path+'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'mediaThumbQueue',
		'auto'           : true,
		'multi'          : false ,
		'queueSizeLimit' : 1 ,
		'fileDesc' 		 : '*.jpg;*.jpeg;*.png;', 
		'fileExt'		 : '*.jpg;*.jpeg;*.png;', 
		'sizeLimit':  8 * 1024 * 1024  , //byte
		'buttonText':'Select Image',
		'buttonImg': uploadButtonImg,
		'width'			 : 120,
		'height'		 : 30,
		onComplete: function (evt, queueID, fileObj, response, data) {
			//response = replaceBom(response);
			
			$('#mediaThumbPhoto').html('<img src="'+ response +'" />');
			fileUploadComplete('#mediaThumbQueue .uploadifyQueueItem');
			return false;
		},
		onSelect : function(){
			 $('#mediaThumbQueue').html('');
		}
	});

};

function fileUploadComplete(obj){
	$(obj).addClass('onComplete');
}




function mediaFileSource(){
	$("#mediaFileSource").uploadify({
		'uploader'       : root_path+'upload/scripts/uploadify.swf',
		'script'         : root_path +'?option=uploadFileAction%26type=action%26do=uploadFile%26time='+ time(),
		'cancelImg'      : root_path+'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'mediaFileSourceQueue',
		'auto'           : true,
		'multi'          : false ,
		'queueSizeLimit' : 50 ,
		'fileDesc' 		 : '*.zip;*.rar;*.gzip' , 
		'fileExt'		 : '*.zip;*.rar;*.gzip' , 
		'sizeLimit'		 : 300 * 1000 * 1024  , //byte
		'buttonText'	 :'Select File',
		'buttonImg'	 	 : uploadButtonImg,
		'width'			 : 120,
		'height'		 : 30,
		onCancel: function (a, b, c, d) {
		},
		onComplete: function (evt, queueID, fileObj, response, data) {
			response = replaceBom(response);
			$('#mediaFileSource'+queueID).attr('file',response);
			fileUploadComplete('#mediaFileSource'+queueID);
			return false;
		},
		onSelect : function(){
			if( $('#mediaFileSourceQueue .uploadifyQueueItem').length >= 3 ){
				$.openWin({'status':'error','msg':'最多只能上传三个文件.','time':2000});
				return false;
			}
		},
		onProgress: function(){
			
		}
	});
};

$('.cancel').live('click',function(){
	 return false;

});


function uploadSubmit(obj){
	if(checkUpload()) submitUploadData(obj);
	return false;	
};

function checkUpload(){
	$uploadForm = $('#uploadForm');
	
	
	var $tagMenu = $uploadForm.find('.tagMenu');
	var tagMenuLen = $tagMenu.length;
	$tagMenu.each(function(i){
		if( i < tagMenuLen - 1 ){
			$uploadForm.attr('allow','OK');
			if( $(this).find('a.active').length == 0 ){
				$uploadForm.attr('allow','error');
			}
		}
	});
	if( $('#fileQueue .uploadifyQueueItem').length == 0 ){
		$.openWin({'msg':'-_-.不能提交.您必须上传一个[文件].','status':'error','time':2000});
		return false;
	}
	if( $('#fileName').val() == '' ){
		$.openWin({'msg':'-_-.不能提交.您必须填写[名称].','status':'error','onClose':function(){$('#fileName').focus();}}); //
		return false;
	};
	if( $uploadForm.attr('allow') != 'OK' && $('#tagList a').length == 0  ){
		$.openWin({'msg':'-_-.不能提交.至少需要选一个标签，或者添加一个自定义标签.','status':'error'});
		return false;
	}
	
	if( $('div.uploadifyQueueItem').size() != $('div.onComplete').size() ){
		$.openWin({'msg':'-_-.不能提交.文件正在上传,不能提交.','status':'error'});
		return false;
	}
	
	
	
	/*
	if( $('#mediaThumbPhoto img').length == 0 ){
		$.openWin({'msg':'-_-.不能提交.您必须上传一张<span class="high">缩略图</span>.','status':'error'});
		return false;
	}
	if( $('#description').val().length == 0 || $('#description').val().length <= 10 ){
		$.openWin({'msg':'-_-.不能提交.您必须填写<span class="high">描述</span>. (10字以上)','status':'error'});
		return false;
	}
	*/
	return true;
}

function submitUploadData(obj){
	var tagMenu = getMenu(obj);	
	var tagCustomMenu = getCustomMenu('#tagList a');
	
	var file = '';
	$('#fileQueue .uploadifyQueueItem').each(function(){
		file += '|' + $(this).attr('file');
	});
	file = file.replace('|','');
	
	
	
	
	var fileTitle = '';
	$('#fileQueue span.fileName input').each(function(){
		fileTitle += splitString + $(this).val();
	});
	fileTitle = fileTitle.replace(splitString,'');	
	
	
	
	
	var fileDescription = '';
	$('#fileQueue div.mediaTitle textarea').each(function(){
		fileDescription += splitString + $(this).val();
	});
	fileDescription = fileDescription.replace(splitString,'');	
	
	
	var mediaSource ='';
	$('#mediaFileSourceQueue .uploadifyQueueItem').each(function(){
		mediaSource += '|' + $(this).attr('file');
	});
	mediaSource = mediaSource.replace('|','');
	
	var mediaSourceTitles = '';
	$('#mediaFileSourceQueue span.fileName input').each(function(){
		mediaSourceTitles += splitString + $(this).val();
	});
	mediaSourceTitles = mediaSourceTitles.replace(splitString,'');	
	
	
	var mediaThumbPhoto = $('#mediaThumbPhoto img').attr('src');
	mediaThumbPhoto ? '' : mediaThumbPhoto = '';
	
	
	$obj.find('.submit').attr('disabled',true);
	var submitData = {
		'dataType'			: 'ajax',
		'fileName'			: $('#fileName').val(),
		'file'				: file ,
		'fileTitle' 		: fileTitle,
		'fileDescription'	: fileDescription,
		'mediaThumbPhoto'	: mediaThumbPhoto,
		'mediaSource'		: mediaSource,
		'mediaSourceTitles'	: mediaSourceTitles,
		'description'		: $('#description').val(),
		'tagMenu'			: tagMenu,		
		'mediaPassWord'		: $('#mediaPassWord').val(),
		'tagCustomMenu'		: tagCustomMenu,
		'task'				: $('#task').val(),
		'file_id'			: $('#file_id').val()
	};
	$.openWin({'msg':loadingBar,'canClose':false });
	$.post($obj.attr('action')+'&time='+time(),submitData,function(data){	
		if('ok'==data.status){
			$.openWin({'msg':data.msg,'canClose':false});
			setTimeout(function(){window.location.href = data.url ;},1500);
		}else{
			$.openWin({'time':1});
			alert(data.msg);
		}
		$obj.find('.submit').attr('disabled',false);
	},'json');
}



function addMediaTitle(obj){
	$(obj).parent().find('.mediaTitle').slideDown('normal');
};

function saveMediaTitle(obj){
	var title = $(obj).parent().slideUp('slow').find('textarea').val();
	if( '' == title )
		$(obj).parent().parent().find('.editMediaTitle').hide();
	else
		$(obj).parent().parent().find('.editMediaTitle').css('display','inline-block');
		
	
};
function editMediaTitle(obj){
	$(obj).parent().find('.mediaTitle').slideDown('normal');
	
};

function creatOrderNum(obj){
	$(obj).each(function(i){
		var $orderNum = $(this).find('.orderNum');
		if($orderNum.get(0))
			$orderNum.html(i+1);
		else
			$(this).prepend('<span class="orderNum">'+(i+1)+'</span>');
	});
};

function cancleFile(obj,creatOrderObj){
	$(obj).live('click',function(){
		$(this).parent().parent().animate({'opacity':'hide','height':0},'slow',function(){
			$(this).remove();
			creatOrderNum(creatOrderObj);
		});
		return false;
	});
};

